using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using YooAsset;

namespace EGEngine
{
    public class ResourceLoader
    {
        public bool IsUsing;
        private readonly ResourcePackage _package;
        private Dictionary<string, AssetHandle> _handleDic;

        public ResourceLoader(ResourcePackage package)
        {
            _package = package;
        }


        public T LoadSync<T>(string location) where T : UnityEngine.Object
        {
            var handle = _package.LoadAssetSync<T>(location);
            _handleDic[location] = handle;
            return (T)handle.AssetObject;
        }

        public async Task<T> LoadAsync<T>(string location) where T : UnityEngine.Object
        {
            var handle = _package.LoadAssetAsync<T>(location);
            _handleDic[location] = handle;
            //在这里释放handle会怎样
            await handle.Task;
            return (T)handle.AssetObject;
        }

        public void Release()
        {
            foreach (var kv in _handleDic)
            {
                kv.Value.Release();
            }

            IsUsing = false;
        }
    }
}